package user

import (
	"gitee.com/binny_w/gin_starter/core"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

func afterUpdate(c *gin.Context) {
	c.Next()
	role, ok := c.Get("new_role")
	if id := c.Param("id"); id != "" && ok {
		_, err := core.Rbac.ClearRolesForUser("user:" + id)
		if err != nil {
			core.Logger.Error("fail to clear role to user", zap.String("userId", id), zap.Error(err))
		}
		if roleStr, ok1 := role.(string); ok1 && roleStr != "" {
			bln, err := core.Rbac.AddRolesForUser("user:"+id, roleStr)
			if !bln || err != nil {
				core.Logger.Error("fail to update role to user", zap.String("userId", id), zap.Bool("bln", bln), zap.Error(err))
			}
		}
	}
}
